package com.excesys.exsecs.access.controller;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.excesys.exsecs.access.dto.AccessSubareaDTO;
import com.excesys.exsecs.access.entity.AccessSubarea;
import com.excesys.exsecs.access.vo.AccessSubareaExcelVO;
import com.excesys.exsecs.access.vo.AccessSubareaVO;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.excel.annotation.RequestExcel;
import com.excesys.exsecs.common.log.annotation.SysLog;
import com.excesys.exsecs.access.service.AccessSubareaService;
import com.excesys.exsecs.common.security.annotation.Inner;
import org.springframework.security.access.prepost.PreAuthorize;
import com.excesys.exsecs.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.*;

/**
 * 区域管理-空间分区
 *
 * @author lxc
 * @date 2024-09-13 14:26:53
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/accessSubarea" )
@Tag(description = "accessSubarea" , name = "区域管理-空间分区管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class AccessSubareaController {

    private final  AccessSubareaService accessSubareaService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param bamSSubarea 区域管理-空间分区
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('access_accessSubarea_view')")
    public R getSubareaPage(Page page, AccessSubareaDTO bamSSubarea) {
        return R.ok(accessSubareaService.getSubareaVosPage(page, bamSSubarea));
    }

    @Operation(summary = "获取树状菜单", description = "获取树状菜单")
    @GetMapping(value = "/tree")
    @PreAuthorize("@pms.hasPermission('access_accessSubarea_view')")
    public R<List<Tree<String>>> getTree(boolean lazy, String parentId) {
        return R.ok(accessSubareaService.treeMenu(lazy, parentId));

    }

    @Operation(summary = "供数据权限用的分区", description = "获取树状菜单")
    @GetMapping(value = "/treeDataScope")
    public R<List<Tree<String>>> getTree2(boolean lazy, String parentId) {
        return R.ok(accessSubareaService.treeMenu2(lazy, parentId));
    }

    /**
     * 根据条件查询所有数据
     * @param bamSSubarea 区域管理-空间分区
     * @return
     */
    @Operation(summary = "根据条件查询不分页", description = "条件查询")
    @PostMapping("/getSubareaBy")
    @PreAuthorize("@pms.hasPermission('access_accessSubarea_view')")
    public R getSubareaBy(@RequestBody AccessSubareaDTO bamSSubarea) {
        return R.ok(accessSubareaService.getSubareaVos(bamSSubarea));
    }

    /**
     * 根据条件查询所有数据
     * @param bamSSubarea 区域管理-空间分区
     * @return
     */
    @Operation(summary = "不分页查询分区和房间", description = "不分页查询分区和房间")
    @GetMapping("/listByWhere")
    public R getlistByWhere(AccessSubarea bamSSubarea) {
        return R.ok(accessSubareaService.list(Wrappers.query(bamSSubarea)));
    }

    @Inner
    @GetMapping("/listByWhereIn")
    public R getlistByWhereIn(AccessSubarea bamSSubarea) {
        return R.ok(accessSubareaService.list(Wrappers.query(bamSSubarea)));
    }

    /**
     * 通过id查询区域管理-空间分区
     * @param subareaId id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{subareaId}")
    @PreAuthorize("@pms.hasPermission('access_accessSubarea_view')")
    public R getById(@PathVariable("subareaId") String subareaId) {
        return R.ok(accessSubareaService.getById(subareaId));
    }

    /**
     * 新增区域管理-空间分区
     * @param bamSSubarea 区域管理-空间分区
     * @return R
     */
    @Operation(summary = "新增区域管理-空间分区", description = "新增区域管理-空间分区")
    @SysLog("新增区域管理-空间分区")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('access_accessSubarea_add')")
    public R save(@RequestBody AccessSubarea bamSSubarea) {
        return R.ok(accessSubareaService.save(bamSSubarea));
    }

    /**
     * 修改区域管理-空间分区
     * @param bamSSubarea 区域管理-空间分区
     * @return R
     */
    @Operation(summary = "修改区域管理-空间分区", description = "修改区域管理-空间分区")
    @SysLog("修改区域管理-空间分区")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('access_accessSubarea_edit')")
    public R updateById(@RequestBody AccessSubarea bamSSubarea) {
        return R.ok(accessSubareaService.updateById(bamSSubarea));
    }

    /**
     * 通过id删除区域管理-空间分区
     * @param subareaId id
     * @return R
     */
    @Operation(summary = "通过id删除区域管理-空间分区", description = "通过id删除区域管理-空间分区")
    @SysLog("通过id删除区域管理-空间分区")
    @DeleteMapping("/{subareaId}")
    @PreAuthorize("@pms.hasPermission('access_accessSubarea_del')")
    public R removeById(@PathVariable String subareaId) {
        AccessSubareaVO subarea = accessSubareaService.selectByPrimaryKey(subareaId);
        if (subarea.getChildrens().size() > 0) {
            accessSubareaService.removeById(subareaId);
            LambdaQueryWrapper<AccessSubarea> wrapper = Wrappers.<AccessSubarea>lambdaQuery()
                    .eq(AccessSubarea::getSubareadCode, subareaId);
            accessSubareaService.remove(wrapper);
        }
        else {
            accessSubareaService.removeById(subareaId);
        }
        return R.ok("删除成功！");
    }

    @Operation(summary = "通过id删除区域管理-空间分区", description = "通过id删除区域管理-空间分区")
    @SysLog("通过id删除区域管理-空间分区")
    @DeleteMapping("/deleteIds")
    @PreAuthorize("@pms.hasPermission('access_accessSubarea_del')")
    public R removeByIds(@RequestBody List<String> ids) {
        return R.ok(accessSubareaService.removeByIds(ids));
    }

    /**
     * 导出excel 表格
     * @return
     */
    @Operation(summary = "导出所有数据", description = "导出所有数据")
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('access_accessSubarea_export')")
    public List<AccessSubareaExcelVO> export(AccessSubareaDTO bamSSubarea) {
        return accessSubareaService.listSubarea(bamSSubarea);
    }

    /**
     * 导入空间分区
     * @param excelVOList 空间分区列表
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('access_accessSubarea_import')")
    public R importSubarea(@RequestExcel List<AccessSubareaExcelVO> excelVOList, BindingResult bindingResult)
            throws ParseException {
        return accessSubareaService.importSubarea(excelVOList, bindingResult);
    }

    @Operation(summary = "自动生成分区编码", description = "自动生成分区编码")
    @GetMapping("/getSubareaCode")
    @PreAuthorize("@pms.hasPermission('access_accessSubarea_view')")
    public R getSubareaCode(String code, int level, String suffix) throws ParseException {
        return R.ok(accessSubareaService.GetSubareaCodeBy(code, level, suffix));
    }

    /**
     * @description: TODO 批量导入子分区 @param @return @throws @author yuan quan @date 2023/6/27
     * 10:51
     */
    @PostMapping("/batch/import")
    @PreAuthorize("@pms.hasPermission('access_accessSubarea_import')")
    public R batchImportSubarea(@RequestExcel List<AccessSubareaExcelVO> excelVOList, BindingResult bindingResult,
                                @RequestParam String subareadCode) throws ParseException {
        return accessSubareaService.batchImportSubarea(excelVOList, bindingResult, subareadCode);
    }
}